package com.Lixiaoqian.PartyHistoryMuseum.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * 检查权限的工具类
 * Created by Fsh on 2016/12/29.
 */

public class PermissionsChecker {
    private  Context mContext;

    public PermissionsChecker(Context context) {
        mContext = context.getApplicationContext();
    }

    // 判断权限集合
    public boolean lacksPermissions(String... permissions) {
        for (String permission : permissions) {
            if (lacksPermission(permission)) {
                return true;
            }
        }
        return false;
    }

    // 判断是否缺少权限
    private boolean lacksPermission(String permission) {
        return ContextCompat.checkSelfPermission(mContext, permission) ==
                PackageManager.PERMISSION_DENIED;
    }

    /**
     * 检测并开启
     * @param context
     * @param permissions 权限列表
     */
    public void checkPsOpen(Activity context, String... permissions) {
        if (Build.VERSION.SDK_INT > 22) {
            if (lacksPermissions(permissions)) {
                ActivityCompat.requestPermissions(context,permissions, 1);
            }

        }
    }

}
